<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>北京联合大学</title>
</head>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://at.alicdn.com/t/font_835526_xufba1hg66.css">
<link rel="stylesheet" href="../..//css/Initialization.css">
<link rel="stylesheet" href="../..//css/commen.css">
<link rel="stylesheet" href="../..//css/password.css">
<body>
<div id="content">
    <div class="initer">
        <!-- 具体内容 -->
        <div class="bread">
            <div class="plain">管理配置</div>
            <div class="plain">
                <i class="iconfont icon-right"></i>
            </div>
            <div class="plain current"><a href="list?pid=0">学年课程管理</a></div>
            <div class="plain">
                <i class="iconfont icon-right"></i>
            </div>
            <div class="plain current">编辑学年</div>
        </div>
        <div class="modifypassword" style="margin-top: 50px;">
            <div class="modifyinput">
                <span class="text">学年名称</span>
                <input type="hidden" id="id" th:value="${depart.id}"/>
                <input type="text" placeholder="请输入学年名称" name="departname" id="departname" th:value="${depart.departname}">
            </div>
            <div class="modifyinput">
                <span class="text">学年序号</span>
                <input type="text" placeholder="请输入学年序号" name="sort" id="sort" th:value="${depart.sort}">
            </div>
            <div class="teacherlookwork teacherlookteam teacherHuiZong">
                <table class="table">
                    <tr class="tabletitle">
                        <td>是否启用 <input type="checkbox" id="allchecks" />全选</td>
                        <td>单元名称</td>
                        <td>责任老师</td>
                    </tr>
                    <tr th:each="item : ${menulist}">
                        <td><input type="checkbox" name="checks" th:value="${item.id}"/> </td>
                        <td th:text="${item.menuname}">单元名称</td>
                        <td>
                            <select id="teacher" name="teacher">
                                <option value="0">请选择</option>
                                <option th:each="person : ${teacherList}" th:value="${person.id}" th:text="${person.realname}">请选择</option>
                            </select>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="modifyinput">
                <span class="center-button" id="subBut">保存</span>
                <span class="center-button" onclick="history.back()">返回</span>
            </div>
        </div>
    </div>
</div>
<script src="../../js/jquery-3.4.1.min.js"></script>
<script src="../../js/commen.js"></script>
<script th:inline="javascript">
    $("#subBut").click(function(){
        var id = $("#id").val();
        var departname = $("#departname").val();
        var sort = $("#sort").val();

        var menuids = "";
        var teachers = "";
        var teacherNames = "";
        $('input[name="checks"]:checked').each(function(){
            menuids += $(this).val() + ",";
            var obj = $(this).parent().parent().find("#teacher");
            teachers += $(obj).val() + ",";
            teacherNames += $(obj).find("option:selected").text() + ",";
        });

        if(departname.length == 0){
            $("#departname").focus();
            alert("请输入组织架构名称");
            return false;
        }else if(sort.length == 0){
            $("#sort").focus();
            alert("请输入组织架构序号");
            return false;
        }else if(menuids.length == 0){
            alert("请选择单元");
            return false;
        }else{
            $.ajax({
                type : "POST",
                url : "toaddgrade",
                data : {id:id, departname: departname, sort:sort, menuids:menuids, teachers: teachers,teacherNames: teacherNames},
                dataType : "json",
                success : function(json) {
                    if(json.state == success){
                        location.href = "list";
                    }else{
                        alert(json.result);
                    }
                },
                error:function(msg){
                    alert("程序出错，请联系管理员");
                }
            });
        }
    });

    $(function () {
        var configs = [[${configs}]];
        console.log(configs)
        $('input[name="checks"]').each(function(){
            for(var i=0;i<configs.length;i++){
                if(configs[i].menuidStr == $(this).val()){
                    $(this).prop("checked", true);
                    $(this).parent().parent().find("#teacher").val(configs[i].personidStr)
                }
            }
        });


        $("#allchecks").click(function () {
            if (this.checked) {
                $("input[name=checks]:checkbox").prop("checked", true);
            } else {
                $("input[name=checks]:checkbox").prop("checked", false);
            }
        });
    });
</script>
</body>
</html>

